package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.IAdjustSalaryBizService;
import com.kk.xhr.admin.controller.request.AdjustSalaryAddRequest;
import com.kk.xhr.admin.controller.request.AdjustSalaryModifyRequest;
import com.kk.xhr.admin.controller.vo.AdjustSalaryVO;
import com.kk.xhr.common.pojo.BinaryEnum;
import com.kk.xhr.web.util.ShiroUtil;
import com.kk.xhr.web.vo.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 调薪管理控制器
 *
 * @author luokexiong
 * @version 1.0 2021/2/19
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "调薪管理", tags = "调薪管理")
@RequestMapping("/adjustSalary")
public class AdjustSalaryController {
    private final Executor adminControllerBizExecutor;
    private final IAdjustSalaryBizService adjustSalaryBizService;

    @ApiOperation(value = "查询我的调薪记录")
    @GetMapping("mine")
    public CompletionStage<ApiResult<List<AdjustSalaryVO>>> queryMine() {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() -> ApiResult.success(adjustSalaryBizService.queryMine(userId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加调薪")
    @PostMapping
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> add(@RequestBody @Valid AdjustSalaryAddRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            adjustSalaryBizService.add(request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改调薪")
    @PutMapping("/{id:.+}")
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> modify(@PathVariable("id") Long id, @RequestBody @Valid AdjustSalaryModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            adjustSalaryBizService.modify(id, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改调薪状态")
    @PutMapping("/status/{id:.+}")
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id, BinaryEnum status) {
        return CompletableFuture.supplyAsync(() -> {
            adjustSalaryBizService.modifyStatus(id, status);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
